package org.example.rxJava;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import rx.Observable;

import java.util.concurrent.TimeUnit;

@Slf4j
public class WindowDemo {

    @Test
    public void simpleWindowObserverDemo() {
        // 最后两个元素，长度<3的话，就直接输出
        Observable.just(1,2,3,4,5,6,7,8)
                .window(3)
                .flatMap(i->i.toList())
                .subscribe(s -> {
                    log.info(String.valueOf(s));
                });
    }

    @Test
    public void windowObserverDemo() {
        Observable.just(1,2,3,4,5,6,7,8)
                .window(3, 1)
                .flatMap(i->i.toList())
                .subscribe(s -> {
                    log.info(String.valueOf(s));
                });
    }

    @Test
    public void timeWindowObserverDemo() throws InterruptedException{
        Observable.interval(100, TimeUnit.MILLISECONDS)
                .window(300, TimeUnit.MILLISECONDS)
                .flatMap(i->i.toList())
                .subscribe(s -> {
                    log.info(s.toString());
                });
        Thread.sleep(Integer.MAX_VALUE);
    }
}
